package com.study.yangfei.mystudy.activity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.study.yangfei.mystudy.ItemFragment;
import com.study.yangfei.mystudy.R;
import com.study.yangfei.mystudy.dummy.DummyContent;
import com.study.yangfei.mystudy.utils.MyService;

public class ListActivity extends AppCompatActivity implements ItemFragment.OnListFragmentInteractionListener {

    MyServiceConnection mConnection = new MyServiceConnection();
    MyService mService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        ItemFragment ifrag = ItemFragment.newInstance(55);
        ifrag.setArguments(getIntent().getExtras());
        this.getSupportFragmentManager().
                beginTransaction().add(R.id.fragment,ifrag).commit();

        startService();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        this.unbindService(mConnection);
    }

    @Override
    public void onListFragmentInteraction(DummyContent.DummyItem item) {

    }

    private void startService(){
        Intent in = new Intent(this, MyService.class);
        this.bindService(in,mConnection, Service.BIND_AUTO_CREATE);
    }

    class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = ((MyService.YfBinder)service).getService();
            mService.heaveyWork();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}
